﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 系统日志
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20 
    /// </summary>
    public class LogBll
    {
        /// <summary>
        /// 静态实例
        /// </summary>
        public static LogBll Instance
        {
            get { return new LogBll(); }
        }

        private LogIService service = new LogService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="category">日志分类</param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<LogEntity>> GetList(int category, PageInput input)
        {
            try
            {
                return await service.GetList(category, input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<LogEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids">id集合</param>
        public async Task Delete(object[] ids)
        {
            try
            {
                await service.Delete(ids);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task<int> Create(LogEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.IPAddress = entity.IPAddress == null ? Net.Ip : entity.IPAddress;
                entity.PlatForm = entity.PlatForm == null ? Net.UserAgent : entity.PlatForm;
                if (string.IsNullOrEmpty(entity.ModuleName) && !string.IsNullOrEmpty(entity.ModuleId))
                    entity.ModuleName = (await new MenuService().GetInfo(entity.ModuleId)).FullName;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="userName">用户名称</param>
        /// <param name="abstracts">摘要</param>
        public async Task WriteLogAsync(string userId, string userName, string abstracts)
        {
            LogEntity entity = new LogEntity();
            entity.UserId = userId;
            entity.UserName = userName;
            entity.Abstracts = abstracts;
            entity.Category = LogSortEnum.Login;
            await this.Create(entity);
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task WriteLogAsync(LogEntity entity)
        {

            await this.Create(entity);

        }
    }
}
